<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class File_m extends FOX_Model
{

    function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $good_id
     * @return mixed
     * 查询单条
     */
    public function get_all_files($limit, $start)
    {

        $this->db->select('*');
        $this->db->from('file');
        $this->db->limit($start, $limit);
        $query = $this->db->get();
        return $query->result_array();
    }

    //添加资讯
    public function add_file($data)
    {
        $this->db->insert('file', $data);
        if ($this->db->affected_rows() > 0) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    // 修改资讯

    public function sava_file($file_id, $data)
    {
        $this->db->where('id', $file_id);
        $this->db->update('file', $data);
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    public function get_file_by_id($file_id)
    {
        $this->db->select('*');
        $this->db->from('file');
        $this->db->where('id', $file_id);
        $query = $this->db->get();
        return $query->row_array();
    }

    //查找前5条按照被推荐的文章  按照时间先后排序
    public function get_five_file($id = 0)
    {
        $this->db->select('*')->from('file')->where('file_god', 1)->where('file_id <>', $id)->limit(5)->order_by('file_id', 'desc');
        $query = $this->db->get();
        return $query->result_array();
    }

    //删除文章
    public function del_file($id)
    {
        $this->db->where('file_id', $id)->delete('file');
    }

    public function get_files(){
        $this->db->select('*');
        $this->db->from('file');
        $this->db->order_by('update_time',"desc");
        $this->db->limit(10);
        $query = $this->db->get();
        return $query->result_array();
    }
}
